<?php
include_once '../inc/db_util.php';

function viewTugas($perkuliahan) {
    $array_res = array();
    $jsArray = json_decode($perkuliahan, false);
    if($jsArray->{'Id'} != NULL){
        $query = mysql_query("SELECT * FROM tb_tugas t 
            JOIN tb_det_matkul dm ON t.id_det_matkul = dm.id_det_matkul
            JOIN tb_matkul_master mm ON mm.id_matkul = dm.id_matkul
            WHERE t.id_det_matkul='" . $jsArray->{'Id'} . "'");
    } else {
        $query = mysql_query("SELECT * FROM tb_tugas t 
            JOIN tb_det_matkul dm ON t.id_det_matkul = dm.id_det_matkul
            JOIN tb_matkul_master mm ON mm.id_matkul = dm.id_matkul
            WHERE t.id_det_matkul IN (
                SELECT id_det_matkul FROM tb_det_matkul WHERE nip = '" . $jsArray->{'Creator'}->{'Nip'} . "'
            ) ORDER BY t.id_det_matkul");
    }
    
    while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
        $row_array['Id'] = $row['id_tugas'];
        $row_array['Judul'] = $row['judul_tugas'];
        $row_array['Deskripsi'] = $row['deskripsi'];
        $row_array['Created'] = $row['tanggal_create'];
        $row_array['Awal'] = $row['tanggal_awal'];
        $row_array['Akhir'] = $row['tanggal_batas'];
        $row_array['DetMatkul']['Id'] = $row['id_det_matkul'];
        $row_array['DetMatkul']['Matkul']['Id'] = $row['id_matkul'];
        $row_array['DetMatkul']['Matkul']['Kode'] = $row['kode_matkul'];
        $row_array['DetMatkul']['Matkul']['Nama'] = $row['matkul'];
        $row_array['DetMatkul']['Matkul']['SKS'] = $row['sks'];
        $row_array['DetMatkul']['Matkul']['Semester'] = $row['semester'];

        array_push($array_res, $row_array);
    }
    return json_encode($array_res);
}

function viewDetail($perkuliahan) {
    $array_res = array();
    $jsArray = json_decode($perkuliahan, false);
    if($jsArray->{'Id'} != NULL){
        $query = mysql_query("SELECT t.id_det_tugas, t.link, t.tanggal_kirim, tm.* 
			FROM tb_det_tugas t 
            JOIN tb_mahasiswa tm ON t.id_mahasiswa = tm.id_mahasiswa
            WHERE t.id_tugas='" . $jsArray->{'Id'} . "'");
    } 
    
    while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
        $row_array['Id'] = $row['id_det_tugas'];
        $row_array['Link'] = $row['link'];
        $row_array['Sent'] = $row['tanggal_kirim'];
        $row_array['Tugas'] = $row['id_tugas'];
        $row_array['Mhs']['Id'] = $row['id_mahasiswa'];
        $row_array['Mhs']['Nim'] = $row['nim'];
        $row_array['Mhs']['Nama'] = $row['nama'];
        $row_array['Mhs']['Username'] = $row['username'];
        $row_array['Mhs']['Alamat'] = $row['alamat'];

        array_push($array_res, $row_array);
    }
    return json_encode($array_res);
}

function viewTugasMhs($perkuliahan) {
    $array_res = array();
    $jsArray = json_decode($perkuliahan, false);
    
    if ($jsArray->{'DetMatkul'}->{'Id'} != NULL) {
        $query = mysql_query("SELECT t.*, dm.*, mm.*, case when tdt.id_det_tugas is not null then 1 else 0 end status,
			tdt.id_det_tugas, tdt.link, coalesce(tdt.tanggal_kirim, now()) tanggal_kirim
			FROM tb_tugas t 
            JOIN tb_det_matkul dm ON t.id_det_matkul = dm.id_det_matkul
            JOIN tb_matkul_master mm ON mm.id_matkul = dm.id_matkul
            LEFT JOIN tb_det_tugas tdt ON t.id_tugas=tdt.id_tugas AND tdt.id_mahasiswa='" . $jsArray->{'Mhs'}->{'Id'} . "'
			WHERE dm.id_det_matkul='" . $jsArray->{'DetMatkul'}->{'Id'} . "'");
    } else {
        $query = mysql_query("SELECT tq.*, dm.*, mm.*, case when tdt.id_det_tugas is not null then 1 else 0 end status,
			tdt.id_det_tugas, tdt.link, coalesce(tdt.tanggal_kirim, now()) tanggal_kirim
			FROM tb_tugas tq
            JOIN tb_det_matkul dm ON tq.id_det_matkul = dm.id_det_matkul
            JOIN tb_matkul_master mm ON mm.id_matkul = dm.id_matkul
			LEFT JOIN tb_det_tugas tdt ON tq.id_tugas=tdt.id_tugas AND tdt.id_mahasiswa='" . $jsArray->{'Mhs'}->{'Id'} . "'
			WHERE dm.id_det_matkul IN (
                SELECT id_det_matkul FROM tb_perkuliahan WHERE id_mahasiswa='" . $jsArray->{'Mhs'}->{'Id'} . "'
            ) ORDER BY dm.id_det_matkul");
    }
    while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
        $row_array['Id'] = $row['id_tugas'];
        $row_array['Judul'] = $row['judul_tugas'];
        $row_array['Deskripsi'] = $row['deskripsi'];
        $row_array['Created'] = $row['tanggal_create'];
        $row_array['Awal'] = $row['tanggal_awal'];
        $row_array['Akhir'] = $row['tanggal_batas'];
        $row_array['Status'] = $row['status'] == 1 ? TRUE : FALSE;
        $row_array['DetailTugas']['Id'] = $row['id_det_tugas'];
        $row_array['DetailTugas']['Link'] = $row['link'];
        $row_array['DetailTugas']['Sent'] = $row['tanggal_kirim'];
        $row_array['DetMatkul']['Id'] = $row['id_det_matkul'];
        $row_array['DetMatkul']['Matkul']['Id'] = $row['id_matkul'];
        $row_array['DetMatkul']['Matkul']['Kode'] = $row['kode_matkul'];
        $row_array['DetMatkul']['Matkul']['Nama'] = $row['matkul'];
        $row_array['DetMatkul']['Matkul']['SKS'] = $row['sks'];
        $row_array['DetMatkul']['Matkul']['Semester'] = $row['semester'];

        array_push($array_res, $row_array);
    }
    return json_encode($array_res);
}

function createTugas($json) {
    $jsArray = json_decode($json, false);

    $sql = "INSERT INTO tb_tugas(id_det_matkul, judul_tugas, deskripsi, tanggal_create, tanggal_awal, tanggal_batas)
        VALUES (            
        '" . $jsArray->{'DetMatkul'}->{'Id'} . "',
        '" . $jsArray->{'Judul'} . "',
        '" . $jsArray->{'Deskripsi'} . "',
        NOW(),
        '" . $jsArray->{'Awal'} . "',
        '" . $jsArray->{'Akhir'} . "'
        )";

    return mysql_query($sql);
}

function updateTugas($json) {
    $jsArray = json_decode($json, false);

    $sql = "UPDATE tb_tugas SET 
        judul_tugas='" . $jsArray->{'Judul'} . "', deskripsi='" . $jsArray->{'Deskripsi'} . "',
        tanggal_awal='" . $jsArray->{'Awal'} . "', tanggal_batas='" . $jsArray->{'Akhir'} . "'
        WHERE id_tugas='" . $jsArray->{'Id'} . "'";

    return mysql_query($sql);
}

function deleteTugas($json) {
    $jsArray = json_decode($json, false);

    $sql = "DELETE FROM tb_tugas WHERE id_tugas='" . $jsArray->{'Id'} . "'";

    return mysql_query($sql);
}

function kerjakanTugas($json) {
	$jsArray = json_decode($json, false);
	$array_res = array();
		
	$tugas = $jsArray->{'Tugas'} ;
	$mhs = $jsArray->{'Mhs'}->{'Id'};
	
    $sql = "INSERT INTO tb_det_tugas(id_tugas, id_mahasiswa, link, tanggal_kirim)
        VALUES (            
        '" . $tugas . "',
        '" . $mhs . "',
        '" . $jsArray->{'Link'} . "',
        NOW()
        )";

    if(mysql_query($sql)){
		$query = mysql_query("SELECT * FROM tb_det_tugas WHERE id_mahasiswa='" . $mhs . "' AND id_tugas = '" . $tugas . "' ORDER BY id_det_tugas DESC");
			
		while ($row = mysql_fetch_array($query, MYSQL_BOTH)) {
			$row_array['Id'] = $row['id_det_tugas'];
			$row_array['Link'] = $row['link'];
			$row_array['Sent'] = $row['tanggal_kirim'];
			$row_array['Tugas'] = $row['id_tugas'];

			array_push($array_res, $row_array);
		}
		return json_encode($array_res);
	}
}

?>
